package com.lzkj.baotouhousingfund.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.coderc.library.view.statusbar.StatusBarUtil;
import com.lzkj.baotouhousingfund.R;
import com.lzkj.baotouhousingfund.base.BaseActivity;
import com.lzkj.baotouhousingfund.presenter.WelcomePresenter;
import com.lzkj.baotouhousingfund.presenter.contract.WelcomeContract;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by codeest on 16/8/15.
 */

public class WelcomeActivity extends BaseActivity<WelcomePresenter> implements WelcomeContract
        .View {

    @BindView(R.id.img_init)
    ImageView mImgInit;
    @BindView(R.id.btn_jump)
    Button    mBtnJump;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        checkPermissions();
        startClock();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initEventAndData() {
    }

    @Override
    protected void initStatusBarDefColor() {
        StatusBarUtil.setTransparentForImageView(this, mImgInit);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    private void checkPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(Manifest.permission
                        .ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.CHANGE_WIFI_STATE,
                Manifest.permission.INTERNET,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.READ_LOGS,
                Manifest.permission.CAMERA)
                .subscribe(permission -> {
                    if (permission.granted) {

                    } else {

                    }
                });
    }


    private void startClock() {
        mBtnJump.setVisibility(View.VISIBLE);
//        Disposable disposable = App.getAppComponent().retrofitHelper().countdown(4)
//                .compose(RxUtil.rxSchedulerHelper())
//                .map(time -> {
//                    if (time == 0) {
//                        return true;
//                    }
//                    mBtnJump.setText(String.format(getString(R.string.jump_countdown_format),
//                            time - 1));
//                    return false;
//                })
//                .subscribe(aBoolean -> {
//                    if (aBoolean) {
//                        mBtnJump.setVisibility(View.GONE);
//                        gotoLoginOrMainActivity();
//                    }
//                });
//        mPresenter.addDisposable(disposable);
        countDownTimer.start();

    }


    //由于CountDownTimer有一定的延迟，所以这里设置3400
    private CountDownTimer countDownTimer = new CountDownTimer(4400, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mBtnJump.setText(String.format(getString(R.string.jump_countdown_format),
                    millisUntilFinished / 1000 - 1));
        }

        @Override
        public void onFinish() {
            mBtnJump.setVisibility(View.GONE);
            mBtnJump.performClick();
        }
    };

    @OnClick(R.id.btn_jump)
    public void gotoLoginOrMainActivity() {
        countDownTimer.cancel();
//        if (TextUtils.isEmpty(SharedPreferenceUtil.getToken())) {
//            gotoLoginActivity();
//        } else {
        gotoMainActivity();
//        }
    }

    private void gotoMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void gotoLoginActivity() {
//        LoginActivity.start(this);
        finish();
    }


    @Override
    public void showError(String msg) {

    }
}
